﻿using System;
using System.IO;
using System.Web.Services.Protocols;
using System.Xml;
using System.Globalization;

namespace CSP.CRM.ISVExtensions
{

  /// <summary>
  /// Shows a virtual earth map of case and contact information. Called from the cases grid.
  /// </summary>
  public partial class VirtualEarthPage : System.Web.UI.Page
  {
    protected CspInfo csp;

    protected void Page_Load(object sender, EventArgs e)
    {
      try
      {
        string organizationName = null;
        if (Request["orgname"] != null)
          organizationName = Request["orgname"];
        int languageCode = 1033;
        if (Request["userlcid"] != null)
          languageCode = Int32.Parse(Request["userlcid"], CultureInfo.InvariantCulture);

        System.Net.IPHostEntry ipEntries = System.Net.Dns.GetHostEntry(Request.Url.Host);
        string strServerIp = ipEntries.AddressList[0].ToString();
        string crmServerUrl = String.Format(CultureInfo.InvariantCulture, "{0}://{1}:{2}", Request.Url.Scheme, strServerIp, Request.Url.Port);

        csp = new CspInfo(new Uri(crmServerUrl), organizationName, languageCode);

        if (Request["pb"] == null)
          return;

        Response.ContentType = "text/xml";
        Response.Clear();

        string[] caseIds = GetSelectedRecords();
        string resultset = csp.GetCaseCollectionInfo(caseIds);
        Response.Write(resultset);
      }
      catch (SoapException soapEx)
      {
        Response.Write("<crmError>" + soapEx.Detail.OuterXml + "</crmError>");
      }
    }

    protected string LocalizeUserInterface()
    {
      string localizeScript = "<script type='text/jscript'>";
      string localizedLabel = csp.GetConfigText("VirtualEarthContactDetails");
      localizeScript += String.Format("labContactDetails = '{0}';", localizedLabel);
      
      localizedLabel = csp.GetConfigText("VirtualEarthCaseDetails");
      localizeScript += String.Format("labCaseDetails = '{0}';", localizedLabel);

      localizedLabel = csp.GetConfigText("VirtualEarthSeeDetails");
      localizeScript += String.Format("labSeeDetails = '{0}';", localizedLabel);

      localizedLabel = csp.GetConfigText("VirtualEarthLayerSelector");
      localizeScript += String.Format("labLayerSelector = '{0}';", localizedLabel);

      localizedLabel = csp.GetConfigText("VirtualEarthSatisfiedCitizens");
      localizeScript += String.Format("labSatisfiedCitizens = '{0}';", localizedLabel);

      localizedLabel = csp.GetConfigText("VirtualEarthDisatisfiedCitizens");
      localizeScript += String.Format("labDisatisfiedCitizens = '{0}';", localizedLabel);

      localizedLabel = csp.GetConfigText("VirtualEarthNeutralCitizens");
      localizeScript += String.Format("labNeutralCitizens = '{0}';", localizedLabel);

      localizedLabel = csp.GetConfigText("VirtualEarthUnratedCases");
      localizeScript += String.Format("labUnratedCases = '{0}';", localizedLabel);

      return  localizeScript + "</script>";
    }

    // Request includes case ids separated by ','
    private string[] GetSelectedRecords()
    {
      StreamReader sr = new StreamReader(Request.InputStream);
      string recordsXml = sr.ReadToEnd();

      XmlDocument xmlDoc = new XmlDocument();
      xmlDoc.LoadXml(recordsXml);
      string selectedCases = xmlDoc.DocumentElement.InnerText;

      if (selectedCases == null || selectedCases.Length == 0)
        return new string[0];
      else
        return selectedCases.Split(',');
    }
  }
}
